package com.project.service;

import com.project.dao.*;
import com.project.entity.*;
import com.project.myBeans.ExerciseDetail;
import com.project.service.base.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Transactional(rollbackFor = Exception.class)
@Service
public class AnswerRecordServiceImpl extends BaseService implements AnswerRecordService {

    @Autowired
    AnswerRecordMapper answerRecordMapper;
    @Autowired
    ExamBlankrecordMapper examBlankrecordmapper;
    @Autowired
    ExamBlankrecordService examBlankrecordService;
    @Autowired
    ExamRecordMapper examRecordMapper;
    @Autowired
    ExamRecordService examRecordService;
    @Autowired
    ExamPaperMapper examPaperMapper;
    @Autowired
    ExamPaperService examPaperService;
    @Autowired
    ComposePaperMapper composePaperMapper;
    @Autowired
    ComposePaperService composePaperService;
    @Autowired
    ExamMapper examMapper;
    @Autowired
    ExamService examService;
    @Autowired
    ExerciseInfoMapper exerciseInfoMapper;
    @Autowired
    ExerciseInfoService exerciseInfoService;
    @Autowired
    ChoiceMapper choiceMapper;
    @Autowired
    ChoiceService choiceService;
    @Autowired
    TrueFalseMapper trueFalseMapper;
    @Autowired
    TrueFalseService trueFalseService;
    @Autowired
    CompletionMapper completionMapper;
    @Autowired
    CompletionService completionService;
    @Autowired
    ShortAnswerMapper shortAnswerMapper;
    @Autowired
    ShortAnswerService shortAnswerService;
    @Autowired
    ProgrammingMapper programmingMapper;
    @Autowired
    ProgrammingService programmingService;
    @Autowired
    CompAnswerMapper compAnswerMapper;
    @Autowired
    CompAnswerService compAnswerService;

    @Override
    public void add(AnswerRecord answerRecord) {
        answerRecordMapper.insert(answerRecord);
    }

    @Override
    public List<AnswerRecord> findbythree(Integer studentid, Integer examid, Integer exerciseinfoid) {
        return answerRecordMapper.FindBythree(studentid, examid, exerciseinfoid);
    }

    @Override
    public List<AnswerRecord> findbytwo(Integer examid, Integer exerciseinfoid) {
        return answerRecordMapper.FindBytwo(examid, exerciseinfoid);
    }

    @Override
    public void saveExamAnswer(List<ExerciseDetail> exerciseDetails, Integer examid, Integer studentid) {
        AnswerRecord tanswerRecord = new AnswerRecord();
        tanswerRecord.setStudentid(studentid);
        tanswerRecord.setExamid(examid);
        List<AnswerRecord> answerRecords = answerRecordMapper.selectBatchByCondition(tanswerRecord);
        for (AnswerRecord answerRecord1 : answerRecords)
        {
            int answerrecordid = answerRecord1.getId();
            examRecordMapper.deleteByAnswerrecordid(answerrecordid);
            examBlankrecordmapper.deletebyanswerrecordid(answerrecordid);
            answerRecordMapper.deleteByPrimaryKey(answerrecordid);
        }
        for (ExerciseDetail exerciseDetail : exerciseDetails)
        {
            ExerciseInfo exerciseInfo = exerciseDetail.getExerciseInfo();
            AnswerRecord answerRecord = new AnswerRecord();
            answerRecord.setExerciseinfoid(exerciseInfo.getId());
            answerRecord.setExamid(examid);
            answerRecord.setStudentid(studentid);
            answerRecordMapper.insert(answerRecord);
            if (exerciseInfo.getType() == 4)//填空题答案保存
            {
                List<ExamBlankrecord> blankAnswers = exerciseDetail.getExamBlankrecords();
                for (ExamBlankrecord examBlankrecord: blankAnswers)
                {
                    examBlankrecord.setAnswerrecordid(answerRecord.getId());
                    examBlankrecord.setIstrue(1);
                    examBlankrecordmapper.insert(examBlankrecord);
                }
            } else
            {
                ExamRecord examRecord = exerciseDetail.getExamRecord();
                if ( examRecord!= null)
                {
                    examRecord.setAnswerrecordid(answerRecord.getId());
                    examRecord.setIstrue(1);
                    examRecordMapper.insert(examRecord);
                }
            }

        }
    }

    @Override
    public List<ExerciseDetail> showPage(AttendExam attendExam) {
        int examid = attendExam.getExamid();
        int studentid = attendExam.getStudentid();
        int examPaperid = examService.findById(examid).getExampaperid();
        List<ComposePaper> composePapers = composePaperService.findByPaperId(examPaperid);
        List<ExerciseDetail> exerciseDetailList = new ArrayList<>();
        for (ComposePaper composePaper : composePapers)
        {
            ExerciseDetail exerciseDetail = new ExerciseDetail();
            ExerciseInfo exerciseInfo = exerciseInfoService.findByPrimaryKey(composePaper.getExerinfoid());
            exerciseDetail.setExerciseInfo(exerciseInfo);
            if (exerciseInfo != null)
            {
                AnswerRecord answerRecord = new AnswerRecord();
                answerRecord.setExamid(examid);
                answerRecord.setStudentid(studentid);
                answerRecord.setExerciseinfoid(exerciseInfo.getId());
                Integer answerrecordid = answerRecordMapper.selectBatchByCondition(answerRecord).get(0).getId();
                if(answerrecordid!=null&&answerrecordid!=0)
                {
                    if(exerciseInfo.getType()==4)
                    {
                        exerciseDetail.setExamBlankrecords(examBlankrecordService.findbyAnswerrecordid(answerrecordid));
                    }else
                    {
                        exerciseDetail.setExamRecord(examRecordService.findbyAnswerrecordid(answerrecordid));
                    }
                    switch (exerciseInfo.getType())
                    {
                        case 1: //单选
                        case 2: //多选
                            Choice choice = choiceService.findByExerciseInfoId(composePaper.getExerinfoid());
                            exerciseDetail.setChoice(choice);
                            break;
                        case 3:
                            TrueFalse truefalse = trueFalseService.findByExerciseInfoId(composePaper.getExerinfoid());
                            exerciseDetail.setTrueFalse(truefalse);
                            break;
                        case 4:
                            Completion completion = completionService.findByExerciseInfoId(composePaper.getExerinfoid());
                            String[] strs = compAnswerService.findByCompidOrderByIndex(completion.getId());
                            completion.setBlankCount(strs.length);
                            exerciseDetail.setCompletion(completion);
                            break;
                        case 5:
                            ShortAnswer shortAnswer = shortAnswerService.findByExcerciseInfoId(composePaper.getExerinfoid());
                            exerciseDetail.setShortAnswer(shortAnswer);
                            break;
                        case 6:
                            Programming programming = programmingService.findByExerciseInfoId(composePaper.getExerinfoid());
                            exerciseDetail.setProgramming(programming);
                            break;
                    }
                }
            }
            exerciseDetailList.add(exerciseDetail);
        }
        return exerciseDetailList;
    }
}
